package groovyhostel.zk

import org.zkoss.zul.Listbox
import org.zkoss.zul.Listhead
import org.zkoss.zul.Listitem
import org.zkoss.zul.Listfoot
import org.zkoss.zul.Listcell
import org.zkoss.zk.ui.event.EventListener
import org.zkoss.zul.Div

public class ZkBuilderTest extends GroovyTestCase {
  ZkBuilder zk = new ZkBuilder()

  void testSingleBuilder() {
    Listbox lb = zk.listbox()
    assertNotNull(lb)

    lb = zk.listbox("test")
    assertEquals('test', lb.id)

    lb = zk.listbox(disabled: true)
    assertEquals(true, lb.disabled)

    lb = zk.listbox("happy", height: '14%')
    assertEquals('happy', lb.id)
    assertEquals('14%', lb.height)
  }

  void testNesting(){
    Listbox lb = zk.listbox() {
      listhead("lhtest") {
        listheader(label: 'head1')
        listheader(label: 'head2')        
      }
      listitem('litest', disabled: true) {
        listcell(label: 'label1')
        listcell(label: 'label2')
      }
      listfoot(visible: false) {
      }
    }
    assertEquals(3, lb.children.size())
    Listhead lh = lb.children[0]
    assertEquals(2, lh.children.size())
    assertEquals('lhtest', lh.id)
    assertEquals('head1', lh.children[0].label) 
    Listitem li = lb.children[1]
    assertEquals(2, li.children.size())
    assertTrue(li.disabled) 
    assertEquals('litest', li.id)
    assertEquals('label1', li.children[0].label) 
    Listfoot lf = lb.children[2]
    assertFalse(lf.isVisible())
  }

  void testIdAccessors(){
    Listbox lb = zk.listbox('boxy') {
      boxy.width = '100%'
    }
    assertEquals('100%', lb.width) 
  }

  void testIdClash() {
    try {
      Listbox lb = zk.listbox('listitem')
      fail('Exception expected')
    } catch(IllegalArgumentException expected){
      assertEquals('Cannot use listitem as an ID.  It clashes with class org.zkoss.zul.Listitem', expected.message)
    }
  }

  void testInvalidMethod(){
    shouldFail {
      zk.listboot()
    }
  }

  void testInvalidArguments(){
    //wrong type
    shouldFail {
      zk.listbox('test', 'boot')
    }

    //wrong type
    shouldFail {
      zk.listbox(15)
    }

    //Too many arguments
    shouldFail() {
      zk.listbox('idd', 'money', disabled:true) {
      }
    }
  }

  void testZkComponentPassed() {
    def inner
    def lb = zk.listbox() { Listbox lister ->
      inner = lister
    }
    assertEquals(lb, inner) 
  }

  void testListenerAttributes() {
    def value = null
    def closure = { value = "test" }
    Listcell lc = zk.listcell(onClick: closure)
    EventListener listener = lc.getListenerIterator('onClick').iterator().next()
    listener.onEvent(null) 
    assertEquals('test', value)
  }

  void testListenerProperties() {
    def value = null
    Listcell lc = zk.listcell() {
      onClick {
        value = 'test'
      }
    }
    EventListener listener = lc.getListenerIterator('onClick').iterator().next()
    listener.onEvent(null)
    assertEquals('test', value)    
  }

  void testListenerPropertiesDelegation() {
    Div testDiv = zk.div() {
      div('innerDiv')
      button() {
        onClick {
          def testLabel = label(value: 'Here')
          assertNull(testLabel.parent)
          testLabel.parent = innerDiv
        }
      }
    }
    def listener = testDiv.children[1].getListenerIterator('onClick').iterator().next()
    listener.onEvent(null)
    assertEquals('Here', testDiv.children[0].children[0].value)
  }
}